package com.book.controller;


import com.book.service.BookService;

import com.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@RestController
//区分请求的角色 在网关的过滤器起作用

public class BookController {


    @Qualifier("bookServiceImpl")
    @Autowired
    BookService bookeService;

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    @RequestMapping("/admin/getBook")
    public UIResult<List<Book>> getAdminBook(BookVo bookVo) {

        return getAllBook(bookVo);
    }

    @RequestMapping("/admin/deletebook")
    public ResultMap deleteBook(@RequestParam("ids[]") int[] ids) {
        ResultMap resultMap = new ResultMap();
        int i = bookeService.deleteBook(ids);
        resultMap.setCode(200);
        return resultMap;
    }

    private UIResult<List<Book>> getAllBook(BookVo bookVo) {
        bookVo.setPageStart(bookVo.getPage(), bookVo.getLimit());
        UIResult<List<Book>> result = new UIResult<>();
        List<Book> lists = bookeService.getBookList(bookVo);
        if (result != null) {
            result.setCode(0);
            result.setMsg("查询有值");
            result.setData(lists);
            result.setCount(bookeService.selectcount(bookVo));
        } else {
            result.setCode(-1);
            result.setMsg("没有查询到数据");
        }
        return result;
    }

    @RequestMapping("/getBook")
    public UIResult<List<Book>> getBook(@RequestBody BookVo bookVo) {

        return getAllBook(bookVo);
    }


    @PostMapping("/admin/addbook")
    public ResultMap addBook(Book book) {
        ResultMap resultMap = new ResultMap();
        int i = bookeService.addBook(book);
        if (i > 0) {
            resultMap.setCode(200);
            resultMap.setMessage("添加成功");
            resultMap.setResult(book.getId());
        } else {
            resultMap.setCode(-1);
            resultMap.setMessage("添加失败");
        }
        return resultMap;
    }

    @Value("${file.location}")
    private String uploadpath;

    @PostMapping("/admin/uploadbookImg")
    public ResultMap uploadFile(MultipartFile file) {
        ResultMap resultMap = new ResultMap<>();
        String filename = file.getOriginalFilename();
        String newFileName = UUID.randomUUID() + "-" + filename;//换文件名 防止覆盖
        File filedir = new File(uploadpath);
        if (!filedir.exists()) //首先检查上传的文件夹是否存在
            filedir.mkdirs(); //不存在就直接创建
        try {
            //上传文件 到指定的文件夹下和指定的新的文件名
            file.transferTo(new File(uploadpath + newFileName));
            resultMap.setCode(200);
            resultMap.setResult(newFileName);
            resultMap.setMessage("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            resultMap.setCode(-1);
            resultMap.setMessage("上传失败");
        }
        return resultMap;
    }

    @RequestMapping("/lendbook")
    public ResultMap lendBook(@RequestParam("ids[]") int[] ids) {
        Arrays.asList(ids);
        ResultMap resultMap = new ResultMap();
        int i = bookeService.lendBook(ids);
        if (i > 0) {
            resultMap.setCode(200);
            resultMap.setMessage("bookdb 更新成功");
        }
        return resultMap;
    }


}

